package com.slfx.order.controller;

import com.alibaba.fastjson.JSON;
import com.slfx.annotation.Idempotent;
import com.slfx.order.dao.OrderMapper;
import com.slfx.order.pojo.Order;
import com.slfx.order.service.OrderService;
import com.slfx.util.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/genToken")
    public String genToken() {
        //生成token,雪花算法
        String token = String.valueOf(idWorker.nextId());
        //token存入到redis中
        redisTemplate.opsForValue().set(token, "0", 30, TimeUnit.MINUTES);
        return token;
    }

    @PostMapping("/addOrder")
    public boolean addOrder(@RequestBody Order order, HttpServletRequest request) {
        //获取令牌
        String token = request.getHeader("token");
        //校验令牌
        try {
            if (redisTemplate.delete(token)) {
                //是正常请求
                order.setId(String.valueOf(idWorker.nextId()));
                order.setCreateTime(new Date());
                order.setUpdateTime(new Date());
                int result = orderService.addOrder(order);
                if (result > 0) {
                    log.info("新增订单成功：{}", JSON.toJSONString(order));
                    return true;
                } else {
                    log.info("新增订单失败：{}", JSON.toJSONString(order));
                    throw new RuntimeException("新增订单失败");
                }
            } else {
                //重复请求
                log.error("repeat request：{}", JSON.toJSONString(order));
                throw new RuntimeException("repeat request");
            }
        } catch (Exception e) {
            log.error("新增订单异常:{}",e);
            throw new RuntimeException(e.getMessage());
        }
    }

    @Idempotent
    @PostMapping("/addOrder2")
    public boolean addOrder2(@RequestBody Order order) {
        order.setId(String.valueOf(idWorker.nextId()));
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        int result = orderService.addOrder(order);
        if (result > 0) {
            log.info("新增订单成功：{}", JSON.toJSONString(order));
            return true;
        } else {
            log.info("新增订单失败：{}", JSON.toJSONString(order));
            throw new RuntimeException("新增订单失败");
        }
    }


    @Resource
    private OrderMapper orderMapper;

    @GetMapping("/getById")
    public Order getById(@RequestParam("id") Long id) {
        Order order = orderMapper.selectByPrimaryKey(id);
        return order;
    }

}
